package com.example.cmpe277stockdata.dataaccess;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Pair;
import android.widget.Toast;

import com.example.cmpe277stockdata.beans.UserBean;
import com.example.cmpe277stockdata.beans.UserPortfolio;
import com.example.cmpe277stockdata.constants.DataConstants;
import com.google.gson.JsonObject;
import com.microsoft.windowsazure.mobileservices.*;

public class AzurePortfolio {

	private MobileServiceClient mClient;
	private MobileServiceJsonTable mTableTableRows;
	Context ctx;
	List<UserPortfolio> result = new ArrayList<UserPortfolio>();

	public AzurePortfolio(Context ctx) throws MalformedURLException {
		this.ctx = ctx;
		mClient = new MobileServiceClient(
				"https://stockdata.azure-mobile.net/",
				"gbRGSggWlHFQwsQmeMgEULUirbGHgk27", ctx);
		mTableTableRows = mClient.getTable("UserPortfolio");

	}

	public void addData(String tickerSymbol, String companyName, int userId, String userName)
	{
		UserPortfolio bean = new UserPortfolio();
		bean.setUserName(userName);
		bean.setCompanyName(companyName);
		bean.setTickerSymbol(tickerSymbol);
		bean.setUserId(String.valueOf(userId));


		mClient.getTable(UserPortfolio.class).insert(bean,
				new TableOperationCallback<UserPortfolio>() {
			public void onCompleted(UserPortfolio entity,
					Exception exception, ServiceFilterResponse response) {
				if (exception == null) {
					Toast.makeText(ctx, "Insert Success",
							Toast.LENGTH_LONG).show();
				} else { // Insert failed
					Toast.makeText(ctx, "Insert Failure",
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	public void removeData(String tickerSymbol,final String companyName, int userId, final String userName)
	{
//		UserPortfolio portfolio = new UserPortfolio();
//		
//		portfolio.setTableId(5);
//		portfolio.setUserId(String.valueOf(12));
//		portfolio.setCompanyName("Cisco");
//		portfolio.setTickerSymbol("CSCO");
//		portfolio.setUserName("sowmya");
		final MobileServiceTable<UserPortfolio> table = mClient.getTable("UserPortfolio", UserPortfolio.class);
		table.where().field("userId").eq(String.valueOf(userId)).execute(new TableQueryCallback<UserPortfolio>() {

			@Override
			public void onCompleted(
					List<UserPortfolio> checkeduser,
					int arg1,
					Exception exception,
					ServiceFilterResponse arg3) {
				if (exception == null) {
					if(checkeduser == null || checkeduser.size() < 1)
					{
						Toast.makeText(ctx, "No associated rows in portfolio table", Toast.LENGTH_LONG).show();					
					}
					else
					{
						for(UserPortfolio portfolio: checkeduser)
						{
							if(portfolio.getCompanyName().equals(companyName) && portfolio.getUserName().equals(userName))
							{
								table.delete(checkeduser.get(0).getTableId(), new TableDeleteCallback() {

									@Override
									public void onCompleted(Exception arg0, ServiceFilterResponse arg1) {
										if(arg0 == null)
										{
											Toast.makeText(ctx, "Company removed from portfolio", Toast.LENGTH_LONG).show();
										}
										else
										{
											Toast.makeText(ctx, "Company remove from portfolio failed", Toast.LENGTH_LONG).show();
										}

									}
								});

							}
							else {
								continue;
							}
						} 
					}
				
				}
				else
					Toast.makeText(ctx, "Oops! Error. Sorry for the inconvenience", Toast.LENGTH_LONG).show();
			}
		//MobileServiceTable<UserPortfolio> table = mClient.getTable("UserPortfolio", UserPortfolio.class);

		
	});
	}

	public List<UserPortfolio> getPortFolioList(int userId)
	{
		MobileServiceTable<UserPortfolio> table = mClient.getTable("UserPortfolio", UserPortfolio.class);
		table.where().field("userId").eq(String.valueOf(userId)).execute(new TableQueryCallback<UserPortfolio>() {

			@Override
			public void onCompleted(
					List<UserPortfolio> checkeduser,
					int arg1,
					Exception exception,
					ServiceFilterResponse arg3) {
				if (exception == null) {
					if(checkeduser == null || checkeduser.size() < 1)
					{
						DataConstants.IS_AZURE_ACTION_COMPLETE = true;
					}
					else
					{
						DataConstants.IS_AZURE_ACTION_COMPLETE = true;
						for(UserPortfolio portfolio : checkeduser)
						{
							UserPortfolio data = new UserPortfolio();
							data.setCompanyName(portfolio.getCompanyName());
							data.setTickerSymbol(portfolio.getTickerSymbol());
							data.setUserName(portfolio.getUserName());
							data.setUserId(portfolio.getId());
							data.setTableId(portfolio.getTableId());
							result.add(data);
						}
						DataConstants.userSelection = result;

					}

				} else {
					DataConstants.IS_AZURE_ACTION_COMPLETE = true;
				}


			}
		});



		return result;
	}
}
